<?php
/*
 * This little program prints a sorted directory 
 * listing, with dirs first and files afterwards.
 */

// Open current directory
if($handle = opendir("./")){
  
  // Loop through all files
  while(false !== ($file = readdir($handle))){
    
    // Ignore hidden files
    if(!preg_match("/^\./", $file)){
      
      // Put dirs in $dirs[] and files in $files[]
      if(is_dir($file)){
        $dirs[] = $file;
      }else{
        $files[] = $file;
      }
    }
  }
  
  // Close directory
  closedir($handle);

  // if $dirs[] exists, sort it and print all elements in it.
  if(is_array($dirs)){
    sort($dirs);
    foreach($dirs as $dir){
      echo "<a href=\"$dir/\" class=linkdir>$dir/</a><br />\n";
    }
  }

print "<br>";

function fsize($file) {
        $a = array("B", "KB", "MB", "GB", "TB", "PB");

        $pos = 0;
        $size = filesize($file);
        while ($size >= 1024) {
                $size /= 1024;
                $pos++;
        }

        return round($size,2)." ".$a[$pos];
} 

  // if $files[] exists, sort it and print all elements in it.
  if(is_array($files)){
    sort($files);
    foreach($files as $file){

        $size = fsize ($file);
        $time = date("Y.m.d H:i", filemtime($file));
      
      if ($file <> 'index.php' ) 
          {
                  echo "<a href=\"$file\" class=link>$file</a> $size; muudetud: $time<br />\n";
      }
    }
  }
}
?> 